package de.game;

import java.util.logging.Level;
import java.util.logging.Logger;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import de.game.util.Util_Date;

public class PaintThread extends Thread{
	
	private String LOG_ID = "PaintThread";
    private SurfaceHolder mSurfaceHolder;
    private Handler mHandler;
    private Context mContext;
    private Paint mLinePaint;
    private Paint blackPaint;
    GameEngine gEngine;
    private boolean gameOver = false;
    

 
    private long sleepTime;		//for consistent rendering  

    private long delay = 60;	//amount of time to sleep for (in milliseconds) 70 

    public PaintThread(SurfaceHolder surfaceHolder, Context context, Handler handler, GameEngine gEngineS) {
            
        //data about the screen
        mSurfaceHolder = surfaceHolder;
        this.mHandler = handler;
        this.mContext = context;

        //standard game painter. Used to draw on the canvas
        mLinePaint = new Paint();
        mLinePaint.setARGB(255, 0, 255, 0);
        //black painter below to clear the screen before the game is rendered
        blackPaint = new Paint();
        blackPaint.setARGB(255, 0, 0, 0);
        //mLinePaint.setAntiAlias(true);

        gEngine=gEngineS;
    }


    //This is the most important part of the code. It is invoked when the call to start() is
    //made from the SurfaceView class. It loops continuously until the game is finished or
    //the application is suspended.
    @Override
    public void run() {
    	
    	 //UPDATE
	     while (gEngine.getGameState()==GameEngine.GAME_STATE_RUNNING) {
	         
	    	 //time before update
	    	 GameEngine.currentTime = System.currentTimeMillis();//System.nanoTime();
	         //This is where we update the game engine
	         Log.d(LOG_ID, Util_Date.timeMillisToDateString(GameEngine.currentTime));
	         gEngine.update();
	

        	 //DRAW
		     Canvas c = null;
		     try {
		           //lock canvas so nothing else can use it
		           c = mSurfaceHolder.lockCanvas(null);
		           synchronized (mSurfaceHolder) {
		                //clear the screen with the black painter.
//		                c.drawRect(0, 0, c.getWidth(), c.getHeight(), blackPaint);
		                //This is where we draw the game engine.
		                Log.d(Constants.LOG_ID, "PaintThread draw engine...");
		                gEngine.draw(c);
		           }
		     } finally {
		         // do this in a finally so that if an exception is thrown
		         // during the above, we don't leave the Surface in an
		         // inconsistent state
		         if (c != null) {
		             mSurfaceHolder.unlockCanvasAndPost(c);
		         }
		     }
		
		
		     //SLEEP
		     //Sleep time. Time required to sleep to keep game consistent
		     //This starts with the specified delay time (in milliseconds) then subtracts from that the
		     //actual time it took to update and render the game. This allows our game to render smoothly.
		     this.sleepTime = delay - ((System.currentTimeMillis() - GameEngine.currentTime) / 1000000L);
	
             try {
                //actual sleep code
                if(sleepTime > 0){

                Thread.sleep(sleepTime);

                }
             } catch (InterruptedException ex) {
                Logger.getLogger(PaintThread.class.getName()).log(Level.SEVERE, null, ex);
             }
          
	     }
	     
	     
    }
}
